package com.cgc.venus.server.app.controller;

import com.cgc.venus.server.app.inter.AppStorageService;
import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 业哥 on 2017/12/7.
 * 应用中心-存储管理
 */

@Api(value = "应用中心-存储管理",description="存储管理所对应服务的api")
@RequestMapping("/storage-admin")
@RestController
public class AppStorageController {
    @Autowired
    private AppStorageService appStorageServiceImpl;

    @ApiOperation("存储查询")
    @RequestMapping(value = "/storage-claim", method = RequestMethod.GET)
    public VenusResponse<Object> storageClaim(@RequestParam(value = "queryKey", required = false) String queryKey,@RequestParam(value = "namespace", required = false) String namespace) throws VenusException {
        Object obj = appStorageServiceImpl.getPersistentVolumeClaims(namespace,queryKey);
        VenusResponse<Object> response=new VenusResponse<Object>(VenusResponseStatus.OK,"",obj);
        return response;
    }

    @ApiOperation("可用存储查询")
    @RequestMapping(value = "/storage-claim/available", method = RequestMethod.GET)
    public VenusResponse<Object> storageClaimAvailable(@RequestParam(value = "namespace", required = true) String namespace) throws VenusException {
        Object obj = appStorageServiceImpl.getPersistentVolumeClaimsAvailable(namespace);
        VenusResponse<Object> response=new VenusResponse<Object>(VenusResponseStatus.OK,"",obj);
        return response;
    }

    @ApiOperation("存储验证")
    @RequestMapping(value = "/storage-claim/validate", method = RequestMethod.GET)
    public VenusResponse<Object> storageClaimValidate(@RequestParam("namespace") String namespace,@RequestParam("pvcName") String pvcName) throws VenusException {
        Object obj = appStorageServiceImpl.checkStorageClaimName(pvcName,namespace);
        VenusResponse<Object> response=new VenusResponse<Object>(VenusResponseStatus.OK,"",obj);
        return response;
    }

    @ApiOperation("存储删除")
    @RequestMapping(value = "/storage-claim", method = RequestMethod.DELETE)
    @ResponseBody
    public VenusResponse<Object> deleteStorageClaim(@RequestParam("namespace") String namespace,@RequestParam("pvcName") String pvcName) throws VenusException {
        Object object=appStorageServiceImpl.deletePersistentVolumeClaims(namespace,pvcName);
        VenusResponse<Object> response=new VenusResponse<Object>(VenusResponseStatus.OK,"删除成功",object);
        return response;
    }

    @ApiOperation("存储创建")
    @RequestMapping(value = "/storage-claim", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Object> createStorageClaim(@RequestParam("namespace") String namespace
                                                    ,@RequestParam("pvcName") String pvcName
                                                    ,@RequestParam("pvcAccessMode") String pvcAccessMode
                                                    ,@RequestParam("reqStorageSize") String reqStorageSize) throws VenusException {
        Object object=appStorageServiceImpl.createPersistentVolumeClaims(pvcName,namespace,pvcAccessMode,reqStorageSize);
        VenusResponse<Object> response=new VenusResponse<Object>(VenusResponseStatus.OK,"创建成功",object);
        return response;
    }
}
